package com.yuqingsong.algorithm.sort;

import java.util.Arrays;
import java.util.Comparator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.yuqingsong.algorithm.heap.Heap;

public class SortTest {

	Comparator<Integer> comp = new Comparator<Integer>() {
		@Override
		public int compare(Integer o1, Integer o2) {
			return o1 - o2;
		}
	};

	int length = 40;
	int scope = 10;

	public Integer[] newArray() {
		Integer[] a = new Integer[length];
		for (int i = 0; i < a.length; i++) {
			a[i] = (int) (Math.random() * scope);
		}
		return a;
	}

	/**
	 * @return
	 */
	@DataProvider(name = "sortData")
	public Object[][] arrays() {

		int sortCount = 0;
		int arrayCount = 10;
		@SuppressWarnings("unchecked")
		Sort<Integer>[] sortArray = new Sort[100];
		sortArray[sortCount++] = new InsertSort<Integer>(comp);
		sortArray[sortCount++] = new Heap<Integer>(null, comp);
		sortArray[sortCount++] = new QuickSort<Integer>(comp);
		sortArray[sortCount++] = new MergeSort<Integer>(comp);
		Object[][] returnMe = new Object[sortCount * arrayCount][];
		int k = 0;
		for (int i = 0; i < sortCount; i++) {
			Sort<Integer> s = sortArray[i];
			for (int j = 0; j < arrayCount; j++) {
				returnMe[k++] = new Object[] { new TestData(s) };
			}
		}
		return returnMe;
	}

	@Test(dataProvider = "sortData")
	public void testSort(TestData td) {
		assert Arrays.equals(td.sort.sort(td.array), td.result);
	}

	class TestData {
		Sort<Integer> sort;
		Integer[] array;
		Integer[] result;
		Integer[] orgArray;

		public TestData(Sort<Integer> sort) {
			this.sort = sort;
			this.array = newArray();
			this.orgArray = array.clone();

			this.result = this.array.clone();
			Arrays.sort(this.result, comp);
		}

		@Override
		public String toString() {
			return sort.toString() + ":" + Arrays.toString(this.orgArray);
		}
	}
}
